package com.zhs.study.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhs.study.common.R;
import com.zhs.study.dao.ArticleDao;
import com.zhs.study.dto.ArticleDto;
import com.zhs.study.exception.ServiceException;
import com.zhs.study.pojo.Article;
import com.zhs.study.service.IArticleService;


import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 *
 * @author zhenghuisheng
 * @since 2023.04.18
 */
@Service
@Slf4j
public class ArticleServiceImpl extends ServiceImpl< ArticleDao, Article> implements IArticleService {

    @Resource
    private ArticleDao articleDao;

    @Override
    @Transactional
    public R addOrUpdate(ArticleDto articleDto) {
        if (BeanUtil.isEmpty(articleDto)) throw new ServiceException("数据不能为空");
        Article article = new Article();
        BeanUtil.copyProperties(articleDto,article);
        if (articleDto.getId() == null){
            articleDao.insert(article);
            return R.ok("数据插入成功");
        }else {
            articleDao.updateById(article);
            return R.ok("数据更新成功");
        }
    }
}